/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.filehandlers;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Massimo Canonaco
 *
 */
public class IniLoader {

	/** */
	static final private String COMMENT_TAG = "#";
	
	/** */
	static final private char END_LINE = ';';
	
	/** */
	static final private char GROUP_SEPARATOR = ':';
	
	/** */
	static final private char PATH_SEPARATOR = '+';
	
	/** */
	static final private Pattern LINE_PATTERN = Pattern.compile("((\\w|\\.)*\\w+)*\\s*"+GROUP_SEPARATOR+"\\s*((\\w|\\"+PATH_SEPARATOR+")*(\\.\\w+)?)"+END_LINE);

	/** */
	private Map<String, String> contents;
	
	
	/** */
	public IniLoader(Map<String, String> map)
	{
		contents = map;
	}
	
	/**
	 * @throws IOException */
	public void load(String pathToFile) throws IOException
	{
		BufferedReader fileReader = new BufferedReader(new FileReader(pathToFile));
		String readedLine = null;
		Matcher matcher = null;
		while((readedLine = fileReader.readLine()) != null)
		{
			if(readedLine.startsWith(COMMENT_TAG))
			{
				// It's a comment, do nothing.
			}
			else
			{
				matcher = LINE_PATTERN.matcher(readedLine);
				if(matcher.find())
				{
					contents.put(matcher.group(1), matcher.group(3).replace(PATH_SEPARATOR, File.separatorChar));
				}
			}
		}
		fileReader.close();
	}

	/**
	 * @return the contents
	 */
	public Map<String, String> getContents() {
		return contents;
	}
}
